@extends('app')

@section('content')
    <div class="layui-card" style="margin-bottom: 0;">
        @include('layouts.breadcrumb')

        <div class="layui-tab-content">
            <form class="layui-form">
                <blockquote class="layui-elem-quote quoteBox" >
                    <div class="layui-inline">
                        <div class="layui-input-inline">

                        </div>
                    </div>
                    <a class="layui-btn search_btn" data-type="reload" lay-submit lay-filter="search" style="margin-left: 3px"><i class="layui-icon layui-icon-search"></i>查询</a>
                </blockquote>
            </form>
            <div class="layui-tab-item layui-show">
                <table class="layui-hide" id="model" lay-filter="model"></table>
            </div>
        </div>
    </div>

    {{--表头操作--}}
    <script type="text/html" id="upload">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="edit">添加角色</button>
        </div>
    </script>

    {{--表列操作--}}
    <script type="text/html" id="operate">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
{{--        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="access">分配权限</a>--}}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
@endsection

@section('js')
    <script>
        layui.use('table', function(){
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;

            table.render({
                elem: '#model',
                url:'/role/index',
                cellMinWidth: 95, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                height: 'full-150',
                method : 'POST',
                page: true, //开启分页
                defaultToolbar: ['filter', 'print', 'exports'],
                toolbar:'#upload',
                limit:20,
                loading:true,
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'index', title: '序号',align:'center', fixed: 'left',width:40, type: 'numbers'},
                    {field:'name',title: '角色名称'},
                    {field:'description',title: '角色描述'},
                    {field:'created_at',title: '创建时间',templet:function(item){return dateTime(item.created_at)}},
                    {field:'updated_at',title: '更新时间',templet:function(item){return dateTime(item.updated_at)}},
                    {title: '操作',width:200, align:'center', fixed: 'right', toolbar: '#operate'}
                ]]
            });

            //表头操作
            table.on('toolbar(model)', function(obj){
                switch(obj.event){
                    case 'edit':
                        newWindow('添加角色', '/role/edit','900','650');
                        break;
                };
            });

            //列操作
            table.on('tool(model)',function (obj) {
                if(obj.event === 'del'){//删除行
                    del(obj);
                }else if(obj.event==="access"){
                    var data = obj.data;
                    newWindow('分配权限','/role/access?id='+data.id,500,230);
                }else if(obj.event==="edit"){
                    var data = obj.data;
                    newWindow('编辑角色','/role/edit?id='+data.id,'900','650');
                }
            });

            function del(data) {
                layer.confirm("你确定删除数据吗？如果存在下级导航则一并删除，此操作不能撤销！", {icon: 3, title:'提示'},
                    function(index){//确定回调
                        var id = data.data.id;
                        $.ajax({
                            url:'/role/del',
                            data:{id:id},
                            success:function(data){
                                layer.alert(data.msg,{icon: 1},function () {
                                    location.reload('model');
                                    parent.layer.closeAll();
                                });
                            }
                        })
                    },function (index) {//取消回调
                        layer.close(index);
                    }
                );
            }
        })


    </script>
@endsection
